package cave.utils;

import java.util.ArrayList;

public class CharMap<V> {
	
	private class Pair<V> {
		char key;
		V value;
		
		public Pair(char key, V value) {
			this.key = key; this.value = value;
		}
	}
	
	private final int NUM_BUCKETS = 10;
	
	private ArrayList<Pair<V>>[] buckets;
	
	@SuppressWarnings("unchecked")
	public CharMap() {
		buckets = new ArrayList[NUM_BUCKETS];
		for (int i = 0; i < NUM_BUCKETS; ++i) {
			buckets[i] = new ArrayList<Pair<V>>();
		}
	}
	
	
	public void put(char key, V value) {
		int bucketNum = (int)key % NUM_BUCKETS;
		ArrayList<Pair<V>> bucket = buckets[bucketNum];
		
		for (int i = 0; i < bucket.size(); ++i) {
			Pair<V> pair = bucket.get(i);
			if (pair.key == key) {
				pair.value = value;
				return;
			}
		}

		bucket.add(new Pair<V>(key, value));
	}
	
	public V get(char key) {
		int bucketNum = (int)key % NUM_BUCKETS;
		ArrayList<Pair<V>> bucket = buckets[bucketNum];
		for (int i = 0; i < bucket.size(); ++i) {
			Pair<V> pair = bucket.get(i);
			if (pair.key == key) {
				return pair.value;
			}
		}
		
		return null;
	}
	
}
